package com.swm.util1;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;


/**
 * @author 师文明
 * @data 2025/5/13 下午2:24
 **/
public class HttpUtil {

    public static void main(String[] args) throws IOException {
        // 创建URL对象
        URL url = new URL("https://finance.pae.baidu.com/vapi/v1/getquotation?group=quotation_kline_ab&code=300059&ktype=day");

        // 打开连接
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        // 设置请求方法为GET
        connection.setRequestMethod("GET");
        connection.setReadTimeout(5000); // 设置读取超时为5000毫秒
        connection.setConnectTimeout(5000); // 设置连接超时为5000毫秒

        // 读取响应内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 输出响应内容
        System.out.println("Response Body: " + response.toString());

        // 关闭连接
        connection.disconnect();
    }
}
